package com.shenbianyun.bosskgpay.service.sign;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.constant.MerInfoConstant;
import com.shenbianyun.bosskgpay.entity.Merchant;
import com.shenbianyun.bosskgpay.entity.RequestMessage;
import com.shenbianyun.bosskgpay.entity.ResponseMessage;
import com.shenbianyun.bosskgpay.entity.sign.UserSignQueryReqModel;
import com.shenbianyun.bosskgpay.utils.*;


/**
 * @Author yvan
 * @Date 2021/11/2 16:38:30
 * @Description 调试 自由职业者签约查询 请求6011
 * 可固定业务参数
 **/

public class TestUserSignQueryServiceD  {


    static Merchant mer =  new Merchant();//公共参数
    public static UserSignQueryReqModel proSignQuery = new UserSignQueryReqModel();//业务参数



    public static void main(String[] args) throws Exception {
        //System.out.println(a);
        TestUserSignQueryServiceD a = new TestUserSignQueryServiceD();
        a.runTestUserSignQueryService();

    }

    public String runTestUserSignQueryService() throws Exception {



        /*
         * 商户id
         * */
        mer.setMerid(MerInfoConstant.MER_ID);
        /*
         * 商户私钥
         * */
        mer.setPriKey(MerInfoConstant.MER_PRIVATE_KEY);
        /*
         * 商户公钥
         * */
        mer.setPubKey(MerInfoConstant.MER_PUBLIC_KEY);
        /*
         * 商户签名
         * */
        mer.setInterKey(MerInfoConstant.INTER_KEY);


        //公共参数
        RequestMessage rm = new RequestMessage();
        //接口地址
        rm.setFunCode("6011");
        rm.setMerId(mer.getMerid());
        rm.setVersion(MerInfoConstant.API_VERSION);
        rm.setReqId(rm.getFunCode() + System.currentTimeMillis());


        //业务参数：自由职业者签约查询参数
        proSignQuery.setName("崔方方");
        proSignQuery.setMobile("18001150364");
        proSignQuery.setCardNo(6214830102464327L);
        proSignQuery.setIdCard("412828199310044519");
        proSignQuery.setProviderId(34156);
        //proSignQuery.setProviderId(30481);
        //proSignQuery.setLevyId(31720);

        //reqData转json
        String reqData = JsonUtils.toJson(proSignQuery);
        System.out.println("reqDatajson= " + reqData);


        //reqData加密
        byte[] bs = DESUtils.encrypt(reqData.getBytes(MerInfoConstant.CHARSET_UTF_8), mer.getInterKey());
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, mer.getPriKey()));

        String reqStr = JsonUtils.toJson(rm);
        System.out.println("请求数据：" + reqStr);


        String respData = HttpClientHelper.sendPost(MerInfoConstant.OMS_TEST_URL, "application/json", reqStr, "POST", MerInfoConstant.CHARSET_UTF_8);
        System.out.println("返回数据："+respData);

        //断言
        JSONObject jsonObject1 = JSONObject.parseObject(respData);
        if (jsonObject1.get("resCode").equals("0000") || jsonObject1.get("resMsg").toString().contains("频繁")) {
            System.out.println("签约查询结果:" + true);
        } else {
            System.out.println("签约查询结果:" + false + " " + jsonObject1.get("resMsg"));
        }


        //返回信息：DES 解密
        ResponseMessage responseMessage = JSON.parseObject(respData, ResponseMessage.class);
        /** 2、Base64解编码 resDATA对象解密 **/
        String resData = responseMessage.getResData();
        byte[] base64bs = Base64Utils.decode(resData.getBytes());
        /** 3、DES 解密 **/
        String resultData = DESUtils.decryptToString(base64bs, MerInfoConstant.INTER_KEY);
        responseMessage.setResData(resultData);
        System.out.println("解密结果："+responseMessage);


        return respData;
    }



}
